package cn.com.xf.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.com.xf.dao.TblOaDutyDao;
import cn.com.xf.domain.Tbempinfo;
import cn.com.xf.domain.TblOaDuty;
import cn.com.xf.domain.TblOaDutySchedule;
import cn.com.xf.service.DutyService;
import cn.com.xf.utils.DateUtils;
import cn.com.xf.web.form.DutyForm;

@Service
public class DutyServiceImpl implements DutyService {

	@Autowired
	TblOaDutyDao tblOaDutyDA0;
	
	@Override
	public List<TblOaDuty> selectDutys(TblOaDuty form) {
		
		List<TblOaDuty> list = tblOaDutyDA0.selectDutys(form);
		return list;
	}

	@Override
	public int selectDutyCount(String siteId, String deptId, Date dutyDate) {
		int count = tblOaDutyDA0.selectDutyCount(siteId,deptId,dutyDate);
		return count;
	}

	//构造器
	public DutyServiceImpl() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<TblOaDuty> selectDutys(int begin, int pageSize, String siteId, String deptId, Date dutyDate) {
		// 排除无部门人员
		if (deptId == "D999"||deptId.equals("D999") ) {
			deptId = "";
		}
		List<TblOaDuty> list = tblOaDutyDA0.select(begin, pageSize, siteId, deptId, dutyDate);
		return list;
	}

	@Override
	public TblOaDuty selectDutyById(String id) {
		TblOaDuty duty = tblOaDutyDA0.selectDutyById(id);
		return duty;
	}

	@Override
	public String selectDeptIdByUser(String name) {
		String deptId = tblOaDutyDA0.selectDeptIdByUser(name);
		return deptId;
	}

	@Override
	public Map<String, String> updateDuty(DutyForm form, String realId) throws Exception {
		Map<String, String> errMap = new HashMap<String, String>();
		String errMsg = "";
		// 1.读取数据
		String userId = form.getUserId();
		String dutyDate = form.getDutyDate();
		String scheduleId = form.getScheduleId();
		String enterUserId = form.getEnterUserId();
		
		//2.验证
		if (StringUtils.isBlank(userId)) {
			errMsg = "值班员工未设置";
			errMap.put("errMsg", errMsg);
			return errMap;
		}
		if (StringUtils.isBlank(dutyDate)) {
			errMsg = "值班日期未设置";
			errMap.put("errMsg", errMsg);
			return errMap;
		}
		if (StringUtils.isBlank(scheduleId)) {
			errMsg = "值班班次未设置";
			errMap.put("errMsg", errMsg);
			return errMap;
		}
		if (StringUtils.isBlank(form.getUserTelNo())) {
			errMsg = "员工电话未设置";
			errMap.put("errMsg", errMsg);
			return errMap;
		}
		
		// 3.存储
		TblOaDuty duty = new TblOaDuty();
		duty.setDutyId(realId);
		duty.setUserId(userId);
		duty.setDutyDate(DateUtils.parseDate(dutyDate));
		duty.setScheduleId(scheduleId);
		duty.setMoifyUserId(enterUserId);
		duty.setMoifyTime(new Date());
		tblOaDutyDA0.updateDuty(duty);
		return errMap;
	}

	@Override
	public int deleteDuty(String dutyId) {
		int result = 1;
		tblOaDutyDA0.deleteById(dutyId);
		return result;
	}

	@Override
	public int insertDuty(TblOaDuty duty) {
		tblOaDutyDA0.insert(duty);
		return 1;
	}

	@Override
	public List<Map<String, Object>> datagridSchedules(String siteId, String deptId) {
		List<TblOaDutySchedule> scheduleList = tblOaDutyDA0.selectSchedules(siteId, deptId);
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		if (scheduleList.size() > 0) {
			for (TblOaDutySchedule schedule: scheduleList) {
				Map<String, Object> scheduelMap = new HashMap<String, Object>();
				scheduelMap.put("scheduleId", schedule.getScheduleId());
				scheduelMap.put("scheduleName", schedule.getClassName());
				list.add(scheduelMap);
			}
		}
		return list;
	}

	@Override
	public String datagridTelNo(String userId) {
		TblOaDuty duty = tblOaDutyDA0.selectTelNo(userId);
		if(duty!=null&&duty.getUserTelNo()!=null){
			return duty.getUserTelNo();
		}
		return "";
	}

	@Override
	public int updateUserTelNo(String userId, String userTelNo) {
		int result = tblOaDutyDA0.updateUserTelNo(userId,userTelNo);
		return result;
	}

	@Override
	public List<TblOaDutySchedule> selectSchedules(String siteId, String deptId) {
		List<TblOaDutySchedule> list = tblOaDutyDA0.selectSchedules(siteId, deptId);
		return list;
	}

	@Override
	public TblOaDutySchedule selectScheduleById(String id) {
		TblOaDutySchedule schedule = tblOaDutyDA0.selectScheduleById(id);
		return schedule;
	}

	@Override
	public String datagridDeptTelNo(String siteId,String deptId) {
		TblOaDutySchedule schedule = tblOaDutyDA0.selectDeptTelNo(siteId,deptId);
		if(schedule.getTelNo()!=null){
			return schedule.getTelNo();
		}
		return "";
	}

	@Override
	public TblOaDutySchedule selectScheduleTime(String scheduleId) {
		TblOaDutySchedule schedule = tblOaDutyDA0.selectScheduleById(scheduleId);
		return schedule;
	}

	@Override
	public int updateSchedule(DutyForm form, String realId) {
		// 1.读取数据
		String moifyUserId = form.getMoifyUserId();
		String startTime = form.getStartTime();
		String endTime = form.getEndTime();
		
		// 2.存储
		TblOaDutySchedule schedule = new TblOaDutySchedule();
		schedule.setScheduleId(realId);
		schedule.setStartTime(startTime);
		schedule.setEndTime(endTime);
		schedule.setMoifyUserId(moifyUserId);
		schedule.setMoifyTime(new Date());
		tblOaDutyDA0.updateSchedule(schedule);
		return 1;
	}

	@Override
	public int updateDeptTelNo(String siteId, String deptId, String deptTelNo) {
		int result = tblOaDutyDA0.updateDeptTelNo(siteId,deptId,deptTelNo);
		return result;
	}

	@Override
	public int insertSchedule(TblOaDutySchedule schedule) {
		int count = tblOaDutyDA0.insertSchedule(schedule);
		return count;
	}

	@Override
	public int deleteSchedule(String idStr) {
		int result = 1;
		tblOaDutyDA0.deleteScheduleById(idStr);
		return result;
	}

	@Override
	public List<Map<String, Object>> datagridDepts(String siteId) {
		List<Tbempinfo> deptList = tblOaDutyDA0.selectGetDept(siteId);
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Tbempinfo td = new Tbempinfo();
		td.setDeptid("D99");
		td.setDeptname("其它");
		deptList.add(td);
		for (Tbempinfo dept: deptList) {
			Map<String, Object> deptMap = new HashMap<String, Object>();
			deptMap.put("deptId", dept.getDeptid());
			deptMap.put("deptName", dept.getDeptname());
			list.add(deptMap);
		}	    
		return list;
	}

	@Override
	public List<Map<String, Object>> datagridUsers(String siteId, String deptId) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		List<Tbempinfo> deptList = tblOaDutyDA0.selectGetDept(siteId);
		Tbempinfo td = new Tbempinfo();
		td.setDeptid("D99");
		td.setDeptname("其它");
		deptList.add(td);
		for (Tbempinfo dept: deptList) {			
			if(dept.getDeptid().equals("D99") && deptId.equals("D99")){
				List<Tbempinfo> eList = tblOaDutyDA0.selectEmpBySiteidWithoutDept(siteId);
				for (Tbempinfo e: eList) {
					Map<String, Object> eMap = new HashMap<String, Object>();
					eMap.put("userId", e.getEmpid());
					eMap.put("userName", e.getEmpname()+"("+e.getEmpid()+")");
					list.add(eMap);
				}
			}else if(dept.getDeptid().equals(deptId)){
				List<Tbempinfo> empList = tblOaDutyDA0.selectEmpBySiteIdAndDeptid(siteId, deptId);
				if (empList.size() > 0) {
					for (Tbempinfo emp: empList) {
						Map<String, Object> userMap = new HashMap<String, Object>();
						userMap.put("userId", emp.getEmpid());
						userMap.put("userName", emp.getEmpname()+"("+emp.getEmpid()+")");
						list.add(userMap);
					}
				}
			}
		}
		return list;
	}

	@Override
	public String selectSiteIdByUser(String empId) {
		String checkId = tblOaDutyDA0.selectSiteIdByUser(empId);
		return checkId;
	}

	@Override
	public List<TblOaDuty> copyDutys(TblOaDuty duty) {
		List<TblOaDuty> list = tblOaDutyDA0.copyDutys(duty);
		return list;
	}

}
